import 'package:flutter/material.dart';
import 'tabs/message.dart';
import 'tabs/user.dart';
import 'tabs/category.dart';
import 'tabs/home.dart';
import 'tabs/setting.dart';


class Tabs extends StatefulWidget {
  const Tabs({super.key});

  @override
  State<Tabs> createState() => _TabsState();
}


// 混入SingleTickerProviderStateMixin
class _TabsState extends State<Tabs> {
  // 定义主动触发drawer的key值
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  int _currentIndex = 0;
  // 引入需要的tab
  final List<Widget> _pages =  [
    HomePage(),
    SettingPage(),
    CategoryPage(),
    MessagePage(),
    UserPage()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 绑定key值
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text('FloatingActionButton'),
        leading: IconButton(
            icon: const Icon(Icons.menu),
            onPressed: () {
              print('menu Pressed');
              // 在需要触发drawer的地方打开侧边栏
              _scaffoldKey.currentState?.openDrawer();
            }),
        actions: <Widget>[
          IconButton(
              icon: const Icon(Icons.search),
              onPressed: () {
                print('Search Pressed');
              }),
          IconButton(
              icon: const Icon(Icons.more_horiz),
              onPressed: () {
                print('more_horiz Pressed');
              })
        ],
      ),
      body: Center(
        child: _pages[_currentIndex],
      ),
      bottomNavigationBar: BottomNavigationBar(
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
            BottomNavigationBarItem(icon: Icon(Icons.settings), label: "设置"),
            BottomNavigationBarItem(icon: Icon(Icons.category), label: "分类"),
            BottomNavigationBarItem(icon: Icon(Icons.message), label: "消息"),
            BottomNavigationBarItem(icon: Icon(Icons.people), label: "用户")
          ],
          currentIndex: _currentIndex,
          type: BottomNavigationBarType.fixed,
          onTap: (val) {
            setState(() {
              _currentIndex = val;
            });
          },
          fixedColor: Color.fromRGBO(105, 112, 249, 1)),
      floatingActionButton: (Container(
        height: 48,
        width: 48,
        padding: EdgeInsets.all(0),
        margin: EdgeInsets.only(top: 10),
        decoration: BoxDecoration(
            color: _currentIndex != 2 ? Color.fromRGBO(148, 161, 169, 1) : Color.fromRGBO(105, 112, 249, 1),
            borderRadius: BorderRadius.circular(30)),
        child: FloatingActionButton(
          backgroundColor: _currentIndex != 2 ? Color.fromRGBO(148, 161, 169, 1) : Color.fromRGBO(105, 112, 249, 1),
          onPressed: () {
            setState(() {
              _currentIndex = 1;
            });
          },
          child: Icon(Icons.add),
        ),
      )),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,

      // 配置 侧边抽屉
      drawer: DrawerPage(),
      endDrawer: Drawer(
        child: Text('右侧侧边栏'),
      ),
    );
  }
}


class DrawerPage extends StatelessWidget {
  const DrawerPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Drawer(
        child: Column(
      children: <Widget>[
        UserAccountsDrawerHeader(
          accountName: const Text("大地老师"),
          accountEmail: const Text("dadi@itying.com"),
          currentAccountPicture: const CircleAvatar(
            backgroundImage:
                NetworkImage("https://www.itying.com/images/flutter/3.png"),
          ),
          decoration: const BoxDecoration(
              color: Colors.yellow,
              image: DecorationImage(
                  image: NetworkImage(
                      "https://www.itying.com/images/flutter/2.png"),
                  fit: BoxFit.cover)),
          otherAccountsPictures: <Widget>[
            Image.network("https://www.itying.com/images/flutter/4.png"),
            Image.network("https://www.itying.com/images/flutter/5.png"),
            Image.network("https://www.itying.com/images/flutter/6.png")
          ],
        ),
        ListTile(
          title: Text("个人中心"),
          leading: CircleAvatar(child: Icon(Icons.people)),
        ),
        Divider(),
        ListTile(
          title: Text("系统设置"),
          leading: CircleAvatar(child: Icon(Icons.settings)),
        )
      ],
    ));
  }
}